package io.jft.doll.ui.room.detailFragment

import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.text.Html
import com.bumptech.glide.Glide
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import com.g.base.extend.observeExOnce
import com.g.base.extend.resultNotNull
import com.g.base.room.repository.Status
import io.jft.doll.R
import io.jft.doll.databinding.ViewScrollTextBinding
import io.jft.doll.weidget.UrlDrawable

/**
 * Created by G on 2017/12/21 0021.
 */
class RoomImageFragment : RoomBaseFragment<ViewScrollTextBinding>() {
    override fun setContentView(): Int = R.layout.view_scroll_text
    override fun onLazyLoadOnce() {
        getData()
    }

    override fun getData() {
        roomViewModel.getRoomDetailImage()
                .resultNotNull()
                .observeExOnce(this) {
                    when (it.status) {
                        Status.Loading -> showLoading()
                        Status.Content -> setupView(it.data!!)
                        Status.Error -> showError(it.error?.message)
                    }
                }
    }

    private fun setupView(data: String) {
        showContentView()
        contentView.root.post {
            val viewWidth = contentView.root.width
            if (viewWidth <= 0) {
                if (activity?.isDestroyed == false) setupView(data)
                return@post
            }
            val detail = Html.fromHtml(data, {
                val urlDrawable = UrlDrawable()
                Glide.with(this)
                        .load(it)
                        .asBitmap()
                        .error(R.drawable.load_placeholder)
                        .placeholder(R.drawable.load_placeholder)
                        .into(object : SimpleTarget<Bitmap>() {
                            private fun setDrawableToTextView(bitmap: Bitmap?) {
                                if (bitmap == null || bitmap.width <= 0 || bitmap.height <= 0) return
                                var scale = bitmap.height / bitmap.width.toDouble()
                                if (scale < 0) scale = 1.0
                                val rect = Rect(0, 0, viewWidth, (scale * viewWidth).toInt())
                                val bitmapDrawable = BitmapDrawable(Bitmap.createScaledBitmap(bitmap, rect.width(), rect.height(), true))
                                bitmapDrawable.bounds = rect
                                urlDrawable.drawable = bitmapDrawable
                                urlDrawable.bounds = rect
                                contentView.text.text = contentView.text.text
                                contentView.text.invalidate()
                            }

                            override fun onLoadStarted(placeholder: Drawable?) {
                                setDrawableToTextView((placeholder as? BitmapDrawable)?.bitmap)
                            }

                            override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
                                setDrawableToTextView((errorDrawable as? BitmapDrawable)?.bitmap)
                            }

                            override fun onResourceReady(resource: Bitmap, glideAnimation: GlideAnimation<in Bitmap>?) {
                                setDrawableToTextView(resource)
                            }
                        })
                return@fromHtml urlDrawable
            }, null)
            contentView.text.text = detail
        }
    }
}

